﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using DAL;
namespace EDMS.Users
{
    public partial class UserManager : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {

                if (Request.Params["id"] != null)
                {
                    btnManage.Text = "编辑";
                    repsw.Enabled = false;
                    txtuserid.ReadOnly = true;
                    txtloginpsw.ReadOnly = true;
                    txtloginpsw.Password = false;
                    txtloginpsw.Text = "****";
                    databind();
                }
                else
                {
                    txtloginpsw.Password = true;
                    lblid.Visible = false;
                    txtuserid.Visible = false;
                }

            }
        }

        protected void btnManage_Click(object sender, EventArgs e)
        {
            string name = btnManage.Text;
            switch (name)
            {
                case "添加":
                    Add();
                    break;
                case "编辑":
                    Update();
                    break;
                default:
                    break;
            }
        }
        void Add()
        {

            using (DataClassesDataContext lqDB = new DataClassesDataContext())
            {
                User use = lqDB.User.FirstOrDefault(o => o.UserName == txtusername.Text);
                if (use == null)
                {
                    User user = new User();
                    user.Xjbh = "1001";                 //选举编号
                    user.UserName = txtusername.Text;
                    user.LoginName = txtloginname.Text;
                    user.PassWord = txtloginpsw.Text;
                    user.Phone = txtphone.Text;
                    lqDB.User.InsertOnSubmit(user);
                    lqDB.SubmitChanges();
                    Frame.Global.GlobalFunctions.AlertToUrl("添加成功！", "UserShow.aspx");
                }
                else
                {
                    Frame.Global.GlobalFunctions.Alert("对不起，此用户已存在！");
                    txtloginname.Text = "";
                    txtusername.Text = "";
                }

            }
        }
        void Update()
        { 
            int id = Convert.ToInt32(Request.Params["id"]);
            using (DataClassesDataContext lqDB = new DataClassesDataContext())
            {
                var result = lqDB.User.FirstOrDefault(r => r.ID == id);
                txtuserid.ReadOnly = true;
                result.UserName = txtusername.Text;
                result.LoginName = txtloginname.Text;
                result.Phone = txtphone.Text;
                lqDB.SubmitChanges();
                Frame.Global.GlobalFunctions.AlertToUrl("修改成功！", "UserShow.aspx");
            }
        }
        void databind()
        {
            int id = Convert.ToInt32(Request.Params["id"]);
            using (DataClassesDataContext lqDB = new DataClassesDataContext())
            {
                var user = lqDB.User.FirstOrDefault(u => u.ID == id);
                txtuserid.Text = id.ToString();
                txtusername.Text = user.UserName;
                txtloginname.Text = user.LoginName;
                //txtloginpsw.Text = user.PassWord;
                txtphone.Text = user.Phone;
            }
        }
        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("UserShow.aspx");
        }
    }
}